<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://lib.baomitu.com/jquery/3.6.0/jquery.js"></script>
  </head>
  <body>
    <!-- <div class="test" a="a" data-id="1"></div>
    <script>
      console.log($("div").attr("a")); // a
      console.log($("div").attr("data-id")); // 1
      console.log($("div").data("a")); // undefined
      console.log($("div").data("id")); // 1
    </script> -->
    <p>测试jq中attr()和 data()的区别</p>
    <button class="btn1">点我改变div自定义属性的值</button>
    <button class="btn2">点我查看div自定义属性的值</button>
    <div class="test"></div>
    <script>
      var num = 0;
      $(".btn1").on("click", function () {
        $("div").attr("id", ++num);
      });
      $(".btn2").on("click", function () {
        alert($("div").attr("id"));
      });
    </script>
  </body>
</html>
